home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / bawk.zip / BAWK.C next >
Text File  |  1986-04-15  |  12KB  |  640 lines

  1. /*
  2.  * Bawk main program
  3.  */
  4. #define MAIN 1
  5. #include <stdio.h>
  6. #include "bawk.h"
  7.  
  8. /*
  9.  * Main program
  10.  */
  11. main( argc, argv )
  12. int argc;
  13. char **argv;
  14. {
  15.     char gotrules, didfile, getstdin;
  16.  
  17.     getstdin =
  18.     didfile =
  19.     gotrules = 0;
  20.  
  21.     /*
  22.      * Initialize global variables:
  23.      */
  24.     Beginact =
  25.     Endact =
  26.     Rules =
  27.     Rulep =
  28. #ifdef DEBUG
  29.     Debug =
  30. #endif
  31.     Filename =
  32.     Linecount =
  33.     Saw_break = 0;
  34.     Stackptr = Stackbtm - 1;
  35.     Stacktop = Stackbtm + MAXSTACKSZ;
  36.     Nextvar = Vartab;
  37.  
  38.     strcpy( Fieldsep, " \t" );
  39.     strcpy( Recordsep, "\n" );
  40.  
  41.     /*
  42.      * Parse command line
  43.      */
  44.     while ( --argc )
  45.     {
  46.         if ( **(++argv) == '-' )
  47.         {
  48.             /*
  49.              * Process dash options.
  50.              */
  51.             switch ( tolower( *(++(*argv)) ) )
  52.             {
  53. #ifdef DEBUG
  54.             case 'd':
  55.                 ++Debug;
  56.                 break;
  57. #endif
  58.             case 0:
  59.                 ++getstdin;
  60.                 --argv;
  61.                 goto dosomething;
  62.                 break;
  63.             default: usage();
  64.             }
  65.         }
  66.         else
  67.         {
  68. dosomething:
  69.             if ( gotrules )
  70.             {
  71.                 /*
  72.                  * Already read rules file - assume this is
  73.                  * is a text file for processing.
  74.                  */
  75.                 if ( ++didfile == 1 && Beginact )
  76.                     doaction( Beginact );
  77.                 if ( getstdin )
  78.                 {
  79.                     --getstdin;
  80.                     newfile( 0 );
  81.                 }
  82.                 else
  83.                     newfile( *argv );
  84.                 process();
  85.             }
  86.             else
  87.             {
  88.                 /*
  89.                  * First file name argument on command line
  90.                  * is assumed to be a rules file - attempt to
  91.                  * compile it.
  92.                  */
  93.                 if ( getstdin )
  94.                 {
  95.                     --getstdin;
  96.                     newfile( 0 );
  97.                 }
  98.                 else
  99.                     newfile( *argv );
  100.                 compile();
  101.                 gotrules = 1;
  102.             }
  103.         }
  104.     }
  105.     if ( !gotrules )
  106.         usage();
  107.  
  108.     if ( ! didfile )
  109.     {
  110.         /*
  111.          * Didn't process any files yet - process stdin.
  112.          */
  113.         newfile( 0 );
  114.         if ( Beginact )
  115.             doaction( Beginact );
  116.         process();
  117.     }
  118.     if ( Endact )
  119.         doaction( Endact );
  120. }
  121.  
  122. /*
  123.  * Regular expression/action file compilation routines.
  124.  */
  125. compile()
  126. {
  127.     /*
  128.      * Compile regular expressions and C actions into Rules struct,
  129.      * reading from current input file "Fileptr".
  130.      */
  131.     int c, len;
  132.  
  133. #ifdef DEBUG
  134.     if ( Debug )
  135.         error( "compiling...", 0 );
  136. #endif
  137.  
  138.     while ( (c = getcharacter()) != -1 )
  139.     {
  140.         if ( c==' ' || c=='\t' || c=='\n' )
  141.             /* swallow whitespace */
  142.             ;
  143.         else if ( c=='#' )
  144.         {
  145.             /*
  146.              * Swallow comments
  147.              */
  148.             while ( (c=getcharacter()) != -1 && c!='\n' )
  149.                 ;
  150.         }
  151.         else if ( c=='{' )
  152.         {
  153. #ifdef DEBUG
  154.             if ( Debug )
  155.                 error( "action", 0 );
  156. #endif
  157.             /*
  158.              * Compile (tokenize) the action string into our
  159.              * global work buffer, then allocate some memory
  160.              * for it and copy it over.
  161.              */
  162.             ungetcharacter( '{' );
  163.             len = act_compile( Workbuf );
  164.  
  165.             if ( Rulep && Rulep->action )
  166.             {
  167.                 Rulep->nextrule = getmem( sizeof( *Rulep ) );
  168.                 Rulep = Rulep->nextrule;
  169.                 fillmem( Rulep, sizeof( *Rulep ), 0 );
  170.             }
  171.             if ( !Rulep )
  172.             {
  173.                 /*
  174.                  * This is the first action encountered.
  175.                  * Allocate the first Rules structure and
  176.                  * initialize it
  177.                  */
  178.                 Rules = Rulep = getmem( sizeof( *Rulep ) );
  179.                 fillmem( Rulep, sizeof( *Rulep ), 0 );
  180.             }
  181.             Rulep->action = getmem( len );
  182.             movemem( Workbuf, Rulep->action, len );
  183.         }
  184.         else if ( c==',' )
  185.         {
  186. #ifdef DEBUG
  187.             if ( Debug )
  188.                 error( "stop pattern", 0 );
  189. #endif
  190.             /*
  191.              * It's (hopefully) the second part of a two-part
  192.              * pattern string.  Swallow the comma and start
  193.              * compiling an action string.
  194.              */
  195.             if ( !Rulep || !Rulep->pattern.start )
  196.                 error( "stop pattern without a start",
  197.                     RE_ERROR );
  198.             if ( Rulep->pattern.stop )
  199.                 error( "already have a stop pattern",
  200.                     RE_ERROR );
  201.             len = pat_compile( Workbuf );
  202.             Rulep->pattern.stop = getmem( len );
  203.             movemem( Workbuf, Rulep->pattern.stop, len );
  204.         }
  205.         else
  206.         {
  207.             /*
  208.              * Assume it's a regular expression pattern
  209.              */
  210. #ifdef DEBUG
  211.             if ( Debug )
  212.                 error( "start pattern", 0 );
  213. #endif
  214.  
  215.             ungetcharacter( c );
  216.             len = pat_compile( Workbuf );
  217.  
  218.             if ( *Workbuf == T_BEGIN )
  219.             {
  220.                 /*
  221.                  * Saw a "BEGIN" keyword - compile following
  222.                  * action into special "Beginact" buffer.
  223.                  */
  224.                 len = act_compile( Workbuf );
  225.                 Beginact = getmem( len );
  226.                 movemem( Workbuf, Beginact, len );
  227.                 continue;
  228.             }
  229.             if ( *Workbuf == T_END )
  230.             {
  231.                 /*
  232.                  * Saw an "END" keyword - compile following
  233.                  * action into special "Endact" buffer.
  234.                  */
  235.                 len = act_compile( Workbuf );
  236.                 Endact = getmem( len );
  237.                 movemem( Workbuf, Endact, len );
  238.                 continue;
  239.             }
  240.             if ( Rulep )
  241.             {
  242.                 /*
  243.                  * Already saw a pattern/action - link in
  244.                  * another Rules structure.
  245.                  */
  246.                 Rulep->nextrule = getmem( sizeof( *Rulep ) );
  247.                 Rulep = Rulep->nextrule;
  248.                 fillmem( Rulep, sizeof( *Rulep ), 0 );
  249.             }
  250.             if ( !Rulep )
  251.             {
  252.                 /*
  253.                  * This is the first pattern encountered.
  254.                  * Allocate the first Rules structure and
  255.                  * initialize it
  256.                  */
  257.                 Rules = Rulep = getmem( sizeof( *Rulep ) );
  258.                 fillmem( Rulep, sizeof( *Rulep ), 0 );
  259.             }
  260.             if ( Rulep->pattern.start )
  261.                 error( "already have a start pattern",
  262.                     RE_ERROR );
  263.  
  264.             Rulep->pattern.start = getmem( len );
  265.             movemem( Workbuf, Rulep->pattern.start, len );
  266.         }
  267.     }
  268.     endfile();
  269. }
  270.  
  271. /*
  272.  * Text file main processing loop.
  273.  */
  274. process()
  275. {
  276.     /*
  277.      * Read a line at a time from current input file at "Fileptr",
  278.      * then apply each rule in the Rules chain to the input line.
  279.      */
  280.     int i;
  281.  
  282. #ifdef DEBUG
  283.     if ( Debug )
  284.         error( "processing...", 0 );
  285. #endif
  286.  
  287.     Recordcount = 0;
  288.  
  289.     while ( getline() )
  290.     {
  291.         /*
  292.          * Parse the input line.
  293.          */
  294.         Fieldcount = parse( Linebuf, Fields, Fieldsep );
  295. #ifdef DEBUG
  296.         if ( Debug>1 )
  297.         {
  298.             printf( "parsed %d words:\n", Fieldcount );
  299.             for(i=0; i<Fieldcount; ++i )
  300.                 printf( "<%s>\n", Fields[i] );
  301.         }
  302. #endif
  303.  
  304.         Rulep = Rules;
  305.         do
  306.         {
  307.             if ( ! Rulep->pattern.start )
  308.             {
  309.                 /*
  310.                  * No pattern given - perform action on
  311.                  * every input line.
  312.                  */
  313.                 doaction( Rulep->action );
  314.             }
  315.             else if ( Rulep->pattern.startseen )
  316.             {
  317.                 /*
  318.                  * Start pattern already found - perform
  319.                  * action then check if line matches
  320.                  * stop pattern.
  321.                  */
  322.                 doaction( Rulep->action );
  323.                 if ( dopattern( Rulep->pattern.stop ) )
  324.                     Rulep->pattern.startseen = 0;
  325.             }
  326.             else if ( dopattern( Rulep->pattern.start ) )
  327.             {
  328.                 /*
  329.                  * Matched start pattern - perform action.
  330.                  * If a stop pattern was given, set "start
  331.                  * pattern seen" flag and process every input
  332.                  * line until stop pattern found.
  333.                  */
  334.                 doaction( Rulep->action );
  335.                 if ( Rulep->pattern.stop )
  336.                     Rulep->pattern.startseen = 1;
  337.             }
  338.         }
  339.         while ( Rulep = Rulep->nextrule );
  340.  
  341.         /*
  342.          * Release memory allocated by parse().
  343.          */
  344.         while ( Fieldcount )
  345.             free( Fields[ --Fieldcount ] );
  346.     }
  347. }
  348.  
  349. /*
  350.  * Miscellaneous functions
  351.  */
  352. parse( str, wrdlst, delim )
  353. char *str;
  354. char *wrdlst[];
  355. char *delim;
  356. {
  357.     /*
  358.      * Parse the string of words in "str" into the word list at "wrdlst".
  359.      * A "word" is a sequence of characters delimited by one or more
  360.      * of the characters found in the string "delim".
  361.      * Returns the number of words parsed.
  362.      * CAUTION: the memory for the words in "wrdlst" is allocated
  363.      * by malloc() and should eventually be returned by free()...
  364.      */
  365.     int wrdcnt, wrdlen;
  366.     char wrdbuf[ MAXLINELEN ], c;
  367.  
  368.     wrdcnt = 0;
  369.     while ( *str )
  370.     {
  371.         while ( instr( *str, delim ) )
  372.             ++str;
  373.         if ( !*str )
  374.             break;
  375.         wrdlen = 0;
  376.         while ( (c = *str) && !instr( c, delim ) )
  377.         {
  378.             wrdbuf[ wrdlen++ ] = c;
  379.             ++str;
  380.         }
  381.         wrdbuf[ wrdlen++ ] = 0;
  382.         /*
  383.          * NOTE: allocate a MAXLINELEN sized buffer for every
  384.          * word, just in case user wants to copy a larger string
  385.          * into a field.
  386.          */
  387.         wrdlst[ wrdcnt ] = getmem( MAXLINELEN );
  388.         strcpy( wrdlst[ wrdcnt++ ], wrdbuf );
  389.     }
  390.  
  391.     return wrdcnt;
  392. }
  393.  
  394. unparse( wrdlst, wrdcnt, str, delim )
  395. char *wrdlst[];
  396. int wrdcnt;
  397. char *str;
  398. char *delim;
  399. {
  400.     /*
  401.      * Replace all the words in "str" with the words in "wrdlst",
  402.      * maintaining the same word seperation distance as found in
  403.      * the string.
  404.      * A "word" is a sequence of characters delimited by one or more
  405.      * of the characters found in the string "delim".
  406.      */
  407.     int wc;
  408.     char strbuf[ MAXLINELEN ], *sp, *wp, *start;
  409.  
  410.     wc = 0;        /* next word in "wrdlst" */
  411.     sp = strbuf;    /* points to our local string */
  412.     start = str;    /* save start address of "str" for later... */
  413.     while ( *str )
  414.     {
  415.         /*
  416.          * Copy the field delimiters from the original string to
  417.          * our local version.
  418.          */
  419.         while ( instr( *str, delim ) )
  420.             *sp++ = *str++;
  421.         if ( !*str )
  422.             break;
  423.         /*
  424.          * Skip over the field in the original string and...
  425.          */
  426.         while ( *str && !instr( *str, delim ) )
  427.             ++str;
  428.  
  429.         if ( wc < wrdcnt )
  430.         {
  431.             /*
  432.              * ...copy in the field in the wordlist instead.
  433.              */
  434.             wp = wrdlst[ wc++ ];
  435.             while ( *wp )
  436.                 *sp++ = *wp++;
  437.         }
  438.     }
  439.     /*
  440.      * Tie off the local string, then copy it back to caller's string.
  441.      */
  442.     *sp = 0;
  443.     strcpy( start, strbuf );
  444. }
  445.  
  446. instr( c, s )
  447. char c, *s;
  448. {
  449.     while ( *s )
  450.         if ( c==*s++ )
  451.             return 1;
  452.     return 0;
  453. }
  454.  
  455. char *
  456. getmem( len )
  457. unsigned len;
  458. {
  459.     char *cp;
  460.  
  461.     if ( cp=malloc( len ) )
  462.         return cp;
  463.     error( "out of memory", MEM_ERROR );
  464. }
  465.  
  466. char *
  467. newfile( s )
  468. char *s;
  469. {
  470.     Linecount = 0;
  471.     if ( Filename = s )
  472.     {
  473. #ifdef BDS_C
  474.         if ( fopen( s, Fileptr = Curfbuf ) == -1 )
  475. #else
  476.         if ( !(Fileptr = fopen( s, "r" )) )
  477. #endif
  478.             error( "file not found", FILE_ERROR );
  479.     }
  480.     else
  481.     {
  482.         /*
  483.          * No file name given - process standard input.
  484.          */
  485.         Fileptr = stdin;
  486.         Filename = "standard input";
  487.     }
  488. }
  489.  
  490. getline()
  491. {
  492.     /*
  493.      * Read a line of text from current input file.  Strip off
  494.      * trailing record seperator (newline).
  495.      */
  496.     int rtn, len;
  497.  
  498.     for ( len=0; len<MAXLINELEN; ++len )
  499.     {
  500.         if ( (rtn = getcharacter()) == *Recordsep || rtn == -1 )
  501.             break;
  502.         Linebuf[ len ] = rtn;
  503.     }
  504.     Linebuf[ len ] = 0;
  505.  
  506.     if ( rtn == -1 )
  507.     {
  508.         endfile();
  509.         return 0;
  510.     }
  511.     return 1;
  512. }
  513.  
  514. getcharacter()
  515. {
  516.     /*
  517.      * Read a character from curren input file.
  518.      * WARNING: your getc() must convert lines that end with CR+LF
  519.      * to LF and CP/M's EOF character (^Z) to a -1.
  520.      * Also, getc() must return a -1 when attempting to read from
  521.      * an unopened file.
  522.      */
  523.     int c;
  524.  
  525. #ifdef BDS_C
  526.     /*
  527.      * BDS C doesn't do CR+LF to LF and ^Z to -1 conversions <gag>
  528.      */
  529.     if ( (c = getc( Fileptr )) == '\r' )
  530.     {
  531.         if ( (c = getc( Fileptr )) != '\n' )
  532.         {
  533.             ungetc( c );
  534.             c = '\r';
  535.         }
  536.     }
  537.     else if ( c == 26 )    /* ^Z */
  538.         c = -1;
  539. #else
  540.     c = getc( Fileptr );
  541. #endif
  542.  
  543.     if ( c == *Recordsep )
  544.         ++Recordcount;
  545.     if ( c=='\n' )
  546.         ++Linecount;
  547.  
  548.     return c;
  549. }
  550.  
  551. ungetcharacter( c )
  552. {
  553.     /*
  554.      * Push a character back into the input stream.
  555.      * If the character is a record seperator, or a newline character,
  556.      * the record and line counters are adjusted appropriately.
  557.      */
  558.     if ( c == *Recordsep )
  559.         --Recordcount;
  560.     if ( c=='\n' )
  561.         --Linecount;
  562.     return ungetc( c, Fileptr );
  563. }
  564.  
  565. endfile()
  566. {
  567.     fclose( Fileptr );
  568.     Filename = Linecount = 0;
  569. }
  570.  
  571. error( s, severe )
  572. char *s;
  573. int severe;
  574. {
  575.     char *cp, *errat;
  576.  
  577.     if ( Filename )
  578.         fprintf( stderr, "%s:", Filename );
  579.  
  580.     if ( Linecount )
  581.         fprintf( stderr, " line %d:", Linecount );
  582.  
  583.     fprintf( stderr, " %s\n", s );
  584.     if ( severe )
  585.         exit( severe );
  586. }
  587.  
  588. usage()
  589. {
  590.     error( "Usage: bawk <actfile> [<file> ...]\n", USAGE_ERROR );
  591. }
  592.  
  593. movemem( from, to, count )
  594. char *from, *to;
  595. int count;
  596. {
  597.     while ( count-- > 0 )
  598.         *to++ = *from++;
  599. }
  600.  
  601. fillmem( array, count, value )
  602. char *array, value;
  603. int count;
  604. {
  605.     while ( count-- > 0 )
  606.         *array++ = value;
  607. }
  608.  
  609. strncmp( s, t, n )
  610. char *s, *t;
  611. int n;
  612. {
  613.     while ( --n>0 && *s && *t && *s==*t )
  614.     {
  615.         ++s;
  616.         ++t;
  617.     }
  618.     if ( *s || *t )
  619.         return *s - *t;
  620.     return 0;
  621. }
  622.  
  623. num( c )
  624. char c;
  625. {
  626.     return '0'<=c && c<='9';
  627. }
  628.  
  629. alpha( c )
  630. char c;
  631. {
  632.     return ('a'<=c && c<='z') || ('A'<=c && c<='Z') || c=='_';
  633. }
  634.  
  635. alphanum( c )
  636. char c;
  637. {
  638.     return alpha( c ) || num( c );
  639. }
  640.